class Ejecutivo < ActiveRecord::Base
  has_many :centros, :conditions => {:status_id => 1}
  has_many :corporativos, :conditions => {:status_id => 1}
  has_many :convenios, :conditions => {:status_id => 1}
  has_many :presupuestos, :conditions => {:status_id => 1}
  has_many :concursos, :conditions => {:status_id => 1}
  has_many :evaluaciones, :conditions => {:status_id => 1}
  has_many :promociones, :conditions => {:status_id => 1}
  has_many :presencias, :conditions => {:status_id => 1}
  has_many :salida_materiales, :conditions => {:status_id => 1}

  acts_as_tree :foreign_key => :reporta_a_id
  
  validates_presence_of :nombre, :primer_apellido, :usuario, :contrasenia, :puesto_id, :on => :create
  
  validates_length_of :nombre, :primer_apellido, :segundo_apellido, :maximum => 40, :on => :create
  
  validates_format_of :correo_electronico, :with => /^([a-zA-Z0-9_.+])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/, :on => :create
  
  validates_uniqueness_of :usuario, :on => :create
  
  validates_confirmation_of :contrasenia, :on => :create
  
  def nombre_completo
    "#{nombre} #{primer_apellido} #{segundo_apellido}".strip unless :segundo_apellido.nil?
    
  end
  
  def self.authenticate(usuario, contrasenia)
    conditions = {}
    conditions[:usuario] = usuario
    conditions[:contrasenia] = contrasenia
    conditions[:status_id] = 1
    usuario = Ejecutivo.find(:first, :conditions => conditions)
    
  end

  def password=(pass)
    self.contrasenia = Digest::SHA1.hexdigest(pass)
    self.contrasenia

  end
  
  def self.personal_cuervo
    conditions = {}
    conditions[:puesto_id] = [1,3,4,5,7]
    
    Ejecutivo.find(:all, :conditions => conditions, :order => 'nombre, primer_apellido')
  end

end
